﻿using System;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using FastFood.Business.Entities;
using FastFood.Windows.Helpers;
using FastFood.Windows.Paneles;

namespace FastFood.Windows
{
    public partial class MainForm : KryptonForm
    {
        private readonly FrmExplorador frmExplorador;

        public MainForm()
        {
            InitializeComponent();
            frmExplorador = new FrmExplorador();
            tsbNotificador.ParentForm = this;
        }

        private void exploradorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmExplorador.Show(dockPanel);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                CargaInicial();
            }
        }

        private void permisosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f = new FrmPermisos();
            f.ShowDialog();
        }

        private void datosGeneralesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f = new FrmEmpresa();
            f.ShowDialog();
            try
            {
                this.Text = DatosGenerales.RazonSocial + " - SISTEMA";
                this.lblRazonSocial.Text = DatosGenerales.RazonSocial;
                this.lblRUC.Text = DatosGenerales.RUC;
                this.lblRubro.Text = DatosGenerales.Rubro;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, Constantes.Titulo, ex.Message);
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void autoresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f = new FrmAbout();
            f.ShowDialog();
        }

        private void CargaInicial()
        {
            try
            {
                frmExplorador.Show(dockPanel);
                this.Text = DatosGenerales.RazonSocial + " - SISTEMA";
                this.lblRazonSocial.Text = DatosGenerales.RazonSocial;
                this.lblRUC.Text = DatosGenerales.RUC;
                this.lblRubro.Text = DatosGenerales.Rubro;
                this.lblUsuario.Text = DatosGenerales.PersonalActual.nombreCompleto;
                this.opcionesToolStripMenuItem.Visible = DatosGenerales.UsuarioActual == null ? false : DatosGenerales.UsuarioActual.esPrincipal;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, Constantes.Titulo, ex.Message);
            }
        }

        public void Encontrar<T>(Control control, ref Control resultado) where T : Control
        {
            foreach (Control c in control.Controls)
            {
                if (c is T) resultado = c;
                Encontrar<T>(c, ref resultado);
            }
        }
    }
}
